package com.rulin.asp.business.datacleaning.batchimport;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.rulin.asp.business.datacleaning.util.Response;

@Controller
@RequestMapping("/import")
public class ImportController {
	@Autowired
	private UploadImpl uploadImpl;

	@RequestMapping(value = "/uploadQuestions", method = RequestMethod.POST)
	@ResponseBody
	public Response uploadQuestions(HttpServletRequest request,HttpServletResponse response){
		MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
		Response result = new Response();
		for (Map.Entry<String, MultipartFile> entry :  mRequest.getFileMap().entrySet()) {
			MultipartFile file = entry.getValue();
			
			try {
				result = uploadImpl.batchQuestionUpload(file.getInputStream(), file.getOriginalFilename());
			}catch (Exception e) {
				result.setCode(Response.SERVER_ERROR);
				String message = e.getCause()==null ? e.getMessage() : e.getCause().getMessage();
				result.setMessage(message);
				result.setErrorMessage(e.getMessage());
			}
			continue;
		}
		return result;
	}
}
